package com.example.servlet;

import com.example.dao.UserDAO;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/register")  // 唯一映射路径
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        try {
            UserDAO userDAO = new UserDAO();
            userDAO.insertUser(username, password);
            response.getWriter().println("<h1>注册成功！</h1>");
        } catch (SQLException e) {
            response.getWriter().println("<h1>注册失败：</h1>" + e.getMessage());
            e.printStackTrace();
        }
    }
}